package cz.cernilovsky.android.profileapp.activities;

import java.io.IOException;

import org.json.JSONException;

import cz.cernilovsky.android.profileapp.R;
import cz.cernilovsky.android.profileapp.model.*;
import cz.cernilovsky.android.profileapp.services.ToastService;
import cz.cernilovsky.android.profileapp.storage.ProfilesStorage;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class AddProfileActivity extends Activity {
	/*private static final String NAME = "name";
	private static final String WIFI = "wifi";
	private static final String SOUND = "sounds";
	private static final String BLUETOOTH = "bluetooth";
	private static final String ID = "id";*/
	public static final String PROFILE = "profile";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_profile);
		Bundle extras = getIntent().getExtras();
		if(savedInstanceState == null && extras != null) {
			setTitle(R.string.edit_profile);
			/*((EditText) findViewById(R.id.editTextName)).setText(extras.getString(NAME));
			RadioButton button = (RadioButton) findViewById(extras.getBoolean(WIFI)? R.id.wifiOn : R.id.wifiOff);
			button.setSelected(true);
			button = (RadioButton) findViewById(extras.getBoolean(SOUND)? R.id.soundsOn : R.id.soundsOff);
			button.setSelected(true);
			button = (RadioButton) findViewById(extras.getBoolean(BLUETOOTH)? R.id.bluetoothOn : R.id.bluetoothOff);
			button.setSelected(true);*/
			Profile p = (Profile) extras.getSerializable(PROFILE);
			((EditText) findViewById(R.id.editTextName)).setText(p.getName());
			if(p.getWifi().getOperation() != Operation.NOOP) {
				RadioButton button = (RadioButton) findViewById(p.getWifi().getOperation() == Operation.ON ? R.id.wifiOn : R.id.wifiOff);
				button.setChecked(true);
			}
			if(p.getSounds().getOperation() != Operation.NOOP) {
				RadioButton button = (RadioButton) findViewById(p.getSounds().getOperation() == Operation.ON ? R.id.soundsOn : R.id.soundsOff);
				button.setChecked(true);
			}
			if(p.getBluetooth().getOperation() != Operation.NOOP) {
				RadioButton button = (RadioButton) findViewById(p.getBluetooth().getOperation() == Operation.ON ? R.id.bluetoothOn : R.id.bluetoothOff);
				button.setChecked(true);
			}
		}
	}
	
	public void save(View view) {
		String name = ((EditText) findViewById(R.id.editTextName)).getText().toString().trim();
		if(name.equals("")) {
			Toast.makeText(this, R.string.empty_name, Toast.LENGTH_SHORT).show();
			return;
		}
		
		Action wifi = null, sounds = null, bluetooth = null;
		
		int selected = ((RadioGroup) findViewById(R.id.wifiGroup)).getCheckedRadioButtonId();
		switch(selected) {
		case R.id.wifiNoop:
			wifi = new WiFiAction(Operation.NOOP);
			break;
		case R.id.wifiOn:
			wifi = new WiFiAction(Operation.ON);
			break;
		case R.id.wifiOff:
			wifi = new WiFiAction(Operation.OFF);
			break;
		}
		
		selected = ((RadioGroup) findViewById(R.id.soundsGroup)).getCheckedRadioButtonId();
		switch(selected) {
		case R.id.soundsNoop:
			sounds = new SoundsAction(Operation.NOOP);
			break;
		case R.id.soundsOn:
			sounds = new SoundsAction(Operation.ON);
			break;
		case R.id.soundsOff:
			sounds = new SoundsAction(Operation.OFF);
			break;
		}
		
		selected = ((RadioGroup) findViewById(R.id.bluetoothGroup)).getCheckedRadioButtonId();
		switch(selected) {
		case R.id.bluetoothNoop:
			bluetooth = new BluetoothAction(Operation.NOOP);
			break;
		case R.id.bluetoothOn:
			bluetooth = new BluetoothAction(Operation.ON);
			break;
		case R.id.bluetoothOff:
			bluetooth = new BluetoothAction(Operation.OFF);
			break;
		}
		
		Action actions[] = new Action[] {wifi, sounds, bluetooth};
		for(int i = 0; i < actions.length; i++) {
			if(actions[i].getOperation() == Operation.NOOP) {
				if(i == actions.length-1) {
					Toast.makeText(this, R.string.no_operation_selected, Toast.LENGTH_SHORT).show();
					return;
				}
			} else {
				break;
			}
		}
		
		try {
			if(getIntent().getExtras() == null) {
				Profile profile = new Profile(name, wifi, sounds, bluetooth);
				ProfilesStorage.addProfile(profile, this);
			} else {
				Profile profile = (Profile) getIntent().getExtras().getSerializable(PROFILE);
				profile.setName(name);
				profile.setWifi(wifi);
				profile.setSounds(sounds);
				profile.setBluetooth(bluetooth);
				ProfilesStorage.editProfile(profile, this);
			}
		} catch (IOException e) {
			ToastService.toastSettingsIOError(this);
			e.printStackTrace();
		} catch (JSONException e) {
			ToastService.toastSettingsJSONError(this);
			e.printStackTrace();
		}
		finish();
	}

}
